﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VisionConfig;
using System.Threading;

namespace FalionForm
{
    public delegate void DispTarg(HWindow HalconW);
    public delegate void MouseM(int x,int y);
    public partial class HWindows : UserControl
    {
        public HImage CalibImg;
        public HWindow HW;
        public HWindowControl HWControl;
        public DispTarg dispTarg;
        public   HWndCtrlDes HL = new HWndCtrlDes();     
        public HWindows()
        {
            InitializeComponent();
            HWControl = hW;
            HW = hW.HalconWindow;
            HL.HWndCtrlN(hW);
            dispTarg = HL.dispTarg;
        }
        public void DispImage(HObject Img)
        {
            HL.Repaint(Img);
        }
        public void SetImagePart(HObject Img=null)
        {
            HL.setImagePart(Img);
        }
        public bool DramModel
        {
            set
            { HL.drawModel=value; }
        }
    }
  public  class HWndCtrlDes
    {
        public event MouseM MouseMv;
        public bool drawModel = false;
        private HObject image=new HObject();
        private int stateView;
        private double startX;
        private double startY;
        private HWindowControl viewPort;
        private int windowWidth;
        private int windowHeight;
        private int imageWidth;
        private int imageHeight;
        private double ImgRow1;
        private double ImgCol1;
        private double ImgRow2;
        private double ImgCol2;
        private HWindow ZoomWindow;
        private double zoomWndFactor;
        private double zoomAddOn;
        private int zoomWndSize;
        bool ImagePartFlg = false;//显示区域设置完成标志
        public DispTarg dispTarg;//显示其他配置委托



        /// <summary>
        /// 滚轮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HMouseWheel(object sender, HMouseEventArgs e)///a
        {
            if (!this.drawModel && (image != null) && e.X >= 0 && e.Y >= 0)
            {
                double scale;
                if (e.Delta > 0)
                {
                    scale = 0.9;
                }
                else
                {
                    scale = 1.1111111111111112;
                }
                this.zoomImage(e.X, e.Y, scale);
            }
        }
        private void zoomImage(double x, double y, double scale)///a
        {
            if (!this.drawModel)
            {
                double num = (x - this.ImgCol1) / (viewPort.ImagePart.Width);
                double num2 = (y - this.ImgRow1) / (viewPort.ImagePart.Height);
                double num3 = (viewPort.ImagePart.Width) * scale;
                double num4 = (viewPort.ImagePart.Height) * scale;
                this.ImgCol1 = x - num3 * num;
                this.ImgCol2 = x + num3 * (1.0 - num);
                this.ImgRow1 = y - num4 * num2;
                this.ImgRow2 = y + num4 * (1.0 - num2);
                int num5 = (int)Math.Round(num3);
                int num6 = (int)Math.Round(num4);
                System.Drawing.Rectangle imagePart = this.viewPort.ImagePart;
                imagePart.X = (int)Math.Round(this.ImgCol1);
                imagePart.Y = (int)Math.Round(this.ImgRow1);
                imagePart.Width = ((num5 > 0) ? num5 : 1);
                imagePart.Height = ((num6 > 0) ? num6 : 1);
                this.viewPort.ImagePart = imagePart;
                double num7 = scale * this.zoomWndFactor;
                if (this.zoomWndFactor < 0.01 && num7 < this.zoomWndFactor)
                {
                    this.resetWindow();
                }
                else if (this.zoomWndFactor > 100.0 && num7 > this.zoomWndFactor)
                {
                    this.resetWindow();
                }
                else
                {
                    this.zoomWndFactor = num7;
                    this.repaint();
                }
            }
        }
        double R_start, C_start;
        private void HobjectToHimage(HObject hobject, ref HImage image)
        {
            HTuple pointer, type, width, height;
            HOperatorSet.GetImagePointer1(hobject, out pointer, out type, out width, out height);
            image.GenImage1(type, width, height, pointer);
        }
        protected internal void resetWindow()
        {
            this.ImgRow1 = 0.0;
            this.ImgCol1 = 0.0;
            this.ImgRow2 = (double)this.imageHeight;
            this.ImgCol2 = (double)this.imageWidth;
            this.zoomWndFactor = (double)this.imageWidth / (double)this.viewPort.Width;
            System.Drawing.Rectangle imagePart = this.viewPort.ImagePart;
            imagePart.X = (int)this.ImgCol1;
            imagePart.Y = (int)this.ImgRow1;
            imagePart.Width = this.imageWidth;
            imagePart.Height = this.imageHeight;
            this.viewPort.ImagePart = imagePart;
            repaint();
        }
        /// <summary>
        /// 单击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private bool mousePressed = false;
        private void mouseDown(object sender, HMouseEventArgs e)//a
        {
            if (!this.drawModel)
            {
                this.stateView = 12;
                this.mousePressed = true;
                switch (this.stateView)
                {
                    case 12:
                        this.startX = e.X;
                        this.startY = e.Y;
                        break;
                    case 13:
                        this.activateZoomWindow((int)e.X, (int)e.Y);
                        break;
                }
            }
        }
        private void activateZoomWindow(int X, int Y)//a
        {
            if (this.ZoomWindow != null)
            {
                this.ZoomWindow.Dispose();
            }
            HOperatorSet.SetSystem("border_width", 10);
            this.ZoomWindow = new HWindow();
            double num = ((double)X - this.ImgCol1) / (this.ImgCol2 - this.ImgCol1) * (double)this.viewPort.Width;
            double num2 = ((double)Y - this.ImgRow1) / (this.ImgRow2 - this.ImgRow1) * (double)this.viewPort.Height;
            int num3 = (int)((double)(this.zoomWndSize / 2) * this.zoomWndFactor * this.zoomAddOn);
            this.ZoomWindow.OpenWindow((int)num2 - this.zoomWndSize / 2, (int)num - this.zoomWndSize / 2, this.zoomWndSize, this.zoomWndSize, this.viewPort.HalconID, "visible", "");
            this.ZoomWindow.SetPart(Y - num3, X - num3, Y + num3, X + num3);
            this.repaint();
            this.ZoomWindow.SetColor("black");
        }
        //public void raiseMouseup()
        //{
        //    this.mousePressed = false;
        //}
        /// <summary>
        /// 抬起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mouseUp(object sender, HMouseEventArgs e)//a
        {
            if (!this.drawModel)
            {
                this.mousePressed = false;
            }
        }
        HTuple a, b, c;
        /// <summary>
        /// 移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mouseMoved(object sender, HMouseEventArgs e)///a
        {
            if (!this.drawModel && (image != null))
            {
                try
                {
                    HOperatorSet.GetMposition(viewPort.HalconWindow, out a, out b, out c);
                    MouseMv(a,b);
                }
                catch (Exception) { c = 0; }
                if (this.mousePressed && c == 2)
                {

                    if (this.stateView == 12)
                    {
                        double num = e.X - this.startX;
                        double num2 = e.Y - this.startY;
                        if ((int)num != 0 || (int)num2 != 0)
                        {
                            this.moveImage(num, num2);
                            this.startX = e.X - num;
                            this.startY = e.Y - num2;
                        }
                    }
                }
            }
        }
        private void moveImage(double motionX, double motionY)
        {
            this.ImgRow1 += -motionY;
            this.ImgRow2 += -motionY;
            this.ImgCol1 += -motionX;
            this.ImgCol2 += -motionX;
            System.Drawing.Rectangle imagePart = this.viewPort.ImagePart;
            imagePart.X = (int)Math.Round(this.ImgCol1);
            imagePart.Y = (int)Math.Round(this.ImgRow1);
            this.viewPort.ImagePart = imagePart;
            this.repaint();
        }
        /// <summary>
        /// 窗口重绘
        /// </summary>
        private void repaint()
        {
            this.Repaint();
        }
        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="view"></param>
        public void HWndCtrlN(HWindowControl view)
        {

            this.viewPort = view;
            this.windowWidth = this.viewPort.Size.Width;
            this.windowHeight = this.viewPort.Size.Height;
            this.zoomWndFactor = (double)this.imageWidth / (double)this.viewPort.Width;
            this.zoomAddOn = Math.Pow(0.9, 5.0);
            this.zoomWndSize = 150;
           // HOperatorSet.SetLut(view.HalconWindow, "rainbow");
            this.viewPort.HMouseUp += new HMouseEventHandler(this.mouseUp);
            this.viewPort.HMouseDown += (new HMouseEventHandler(this.mouseDown));
            this.viewPort.HMouseWheel += (new HMouseEventHandler(this.HMouseWheel));
            this.viewPort.HMouseMove += (new HMouseEventHandler(this.mouseMoved));
        }
        /// <summary>
        /// 设置显示区域
        /// </summary>
        /// <param name="imag"></param>
        public void setImagePart(HObject imag = null)//设置显示区域
        {

            if (!(imag != null&&imag.IsInitialized()))
            {

            }
            else
            {
                this.image.Dispose();
                this.image = imag.Clone();

            }
            HTuple Size_w, Size_H;
            double Displ_width1, Displ_height1, Displ_width2, Displ_height2;
            try
            {
                HOperatorSet.GetImageSize(image, out Size_w, out Size_H);
            }
            catch
            {
                return;
            }
            if (Size_w * viewPort.Height >= Size_H * viewPort.Width)
            {
                double temp = Size_w * viewPort.Height / viewPort.Width;
                Displ_width1 = 0;
                Displ_height1 = (temp - Size_H) / 2;
                Displ_width2 = Size_w;
                Displ_height2 = temp - Displ_height1;
            }
            else
            {
                double temp = Size_H * viewPort.Width / viewPort.Height;
                Displ_width1 = (temp - Size_w) / 2;
                Displ_height1 = 0;
                Displ_width2 = temp - Displ_width1;
                Displ_height2 = Size_H;
            }
            HOperatorSet.SetPart(viewPort.HalconWindow, -Displ_height1, -Displ_width1, Displ_height2, Displ_width2);
            ImagePartFlg = true;
            this.ImgRow2 = -Displ_height1 + Size_H;
            this.ImgCol2 = -Displ_width1 + Size_w;
            this.ImgRow1 = -Displ_height1;
            this.ImgCol1 = -Displ_width1;
            C_start = -Displ_width1;
            repaint();
        }
        /// <summary>
        /// 显示图片
        /// </summary>
        /// <param name="images"></param>
        public void Repaint(HObject images = null)///a
        {
            try
            {
                if (images != null)
                {
                    image.Dispose();
                    image = images.Clone();
                }
                if (image == null)
                {
                    return;
                }
                if (ImagePartFlg==false)
                {
                    setImagePart();
                }
                HSystem.SetSystem("flush_graphic", "false");
                viewPort.HalconWindow.ClearWindow();
                viewPort.HalconWindow.DispObj(image);
                if (dispTarg != null)
                {
                    dispTarg(viewPort.HalconWindow);
                }
                HSystem.SetSystem("flush_graphic", "true");
               // viewPort.HalconWindow.SetColor("red");
                viewPort.HalconWindow.DispLine(-100.0, -100.0, -101.0, -101.0);
            }
            catch
            {
                //MessageBox.Show(es.ToString());
            }
        }
    }
}

